﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;
namespace RTSoft.Modules
{
    public class DataProvider
    {
        
        //protected static string _connectionString ;
        private string strConn = ConfigurationManager.ConnectionStrings["sqlConnect"].ToString(); 
        //@"Data Source=.\SQLEXPRESS;Initial Catalog=QLBH;User ID=sa;Password=123456";
        protected SqlConnection con;
        public SqlCommand Command;
        protected SqlDataAdapter adapter;
        public SqlTransaction transaction;
        public static string ConnectionString { get; set; }       
        public SqlConnection Connect(string strEror)
        {
            try
            {
                con = new SqlConnection(strConn);
                con.Open();
                return con;
            }
            catch (Exception ex)
            {
                strEror = ex.Message;
                return con;
            }
        }
        public SqlConnection Connection(ref string strEror)
        {
            try
            {
                con = new SqlConnection(strConn);
                int aaa = con.ConnectionTimeout;
                con.Open();
                int aa = con.ConnectionTimeout;
            }
            catch (SqlException ex)
            {
                strEror = ex.Message;
            }
            return con;
        }
        public void Disconnect()
        {
            con.Dispose();
            con.Close();
        }
        public SqlTransaction BeginTransaction()
        {
            try
            {
                transaction = con.BeginTransaction();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return transaction;
        }
        public void CommitTransaction()
        {
            try
            {
                transaction.Commit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void RollbackTransaction()
        {
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public DataTable LayDanhSach(string strSQL)
        {
            adapter = new SqlDataAdapter(strSQL, strConn);
            DataSet danhsach = new DataSet();
            adapter.Fill(danhsach);
            return danhsach.Tables[0]; 
        }
        public void LayDanhSach(string strSQL, ref DataSet ds, ref DataTable tbl)
        {
            adapter = new SqlDataAdapter(strSQL, ConnectionString);
            adapter.Fill(ds.Tables[tbl.TableName]);
        }
        public IDataReader ExecuteQuery(string strSQL)
        {
            Command = new SqlCommand(strSQL, con);
            return Command.ExecuteReader();
        }
        public void ExecuteNonQuery(string strSQL)
        {
            Command = new SqlCommand(strSQL, con);
            Command.ExecuteNonQuery();
        }
        public object ExecuteScala(string strSQL)
        {

            Command = new SqlCommand(strSQL, con);
            return Command.ExecuteScalar();
        }
    }
}